'use strict';
const Controller = require('egg').Controller;
const qiniu = require('qiniu');


function toInt(str) {
    if (typeof str === 'number') return str;
    if (!str) return str;
    return parseInt(str, 10) || 0;
}
class BaseController extends Controller {


    async getQNToken() {
        let ak = "IYe0gw8UokETMTzlSK90sAhZG8uKU4mvtfOrjNoR",
            sk = "PETigMluzETup4-FzOAxrLNVOadaQ73AdHGW3QpY";
        var mac = new qiniu.auth.digest.Mac(ak, sk);
        var options = {
            scope: 'guoshiying-qiniu',
        };
        var putPolicy = new qiniu.rs.PutPolicy(options);
        var uploadToken = putPolicy.uploadToken(mac);
        return uploadToken
    }
    async getParams() {
        const { ctx } = this;
        ctx.query.limit = ctx.query.limit || 10
        ctx.query.page = ctx.query.page || 1
        let limit = toInt(ctx.query.limit);
        let page = toInt(ctx.query.page);
        let offset = toInt((page - 1) * limit)
        return {
            offset,
            limit
        }
    }
    async success(data) {
        const { ctx } = this;
        ctx.body = {
            code: 200,
            data: data
        }

    }
    async get() {
        const { ctx } = this;
        ctx.body = {
            code: 10000,
            data: await ctx.model.Content.findAll({})
        }

    }
}

module.exports = BaseController;